#!/bin/bash

export BSPDIR=`pwd`

function INFO() {
    echo -e "\033[32mINFO: $*\033[0m"
}

function WARN() {
    echo -e "\033[33mWARN: $*\033[0m"
}

function ERROR() {
    echo -e "\033[31mERROR: $*\033[0m"
}

if [ "$(whoami)" = "root" ]; then
    ERROR "Don't use the BSP as root. Exited!"
    return 1
fi

export MACHINE="qemuriscv64"
export DISTRO="linda-wayland"
export SDKMACHINE="x86_64"

export BUILD_DIR=${BSPDIR}/builds/${MACHINE}
mkdir -p ${BUILD_DIR}

. ./openlayers/poky/oe-init-build-env ${BUILD_DIR} > /dev/null

# if conf/local.conf not generated,
# it is error for exec oe-init-build-env $BUILD_DIR, no need to go further
if [ ! -e conf/local.conf ]; then
    return 1
else
    cp ${BSPDIR}/meta-sifive-autoio/templates/local.conf conf/local.conf
    cp ${BSPDIR}/meta-sifive-autoio/templates/bblayers.conf conf/bblayers.conf

    # Change settings according environment
    sed -e "s,BSPDIR :=.*,BSPDIR := \"${BSPDIR}\",g" -i conf/bblayers.conf
    sed -e "s,MACHINE ??=.*,MACHINE ??= '${MACHINE}',g" \
        -e "s,DISTRO ?=.*,DISTRO ?= '${DISTRO}',g" \
        -e "s,SDKMACHINE ?=.*,SDKMACHINE ?= '${SDKMACHINE}',g" \
        -i conf/local.conf
fi

DL_DIR=`grep "^DL_DIR" conf/local.conf | awk '{print $3}'`
SSTATE_DIR=`grep "^SSTATE_DIR" conf/local.conf | awk '{print $3}'`

INFO "Your build environment has been configured with:

    DISTRO = ${DISTRO}
    MACHINE = ${MACHINE}
    BSPDIR = ${BSPDIR}
    BUILD_DIR = ${BUILD_DIR}
    /
    DL_DIR = ${DL_DIR}
    SSTATE_DIR = ${SSTATE_DIR}
"

